<template>
  <section>
    <!--   工具条   -->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="keyword" placeholder="请输入关键字"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" v-on:click="keywordQuery">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="departments" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="名称" width="200" sortable>
      </el-table-column>
      <el-table-column prop="phone" label="电话" width="120" sortable>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="200" sortable>
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="100" sortable>
      </el-table-column>
      <el-table-column prop="createtime" label="创建时间" width="120" sortable>
      </el-table-column>
      <el-table-column prop="headImg" label="头像" width="100" sortable>
      </el-table-column>

      <el-table-column prop="state" label="状态" width="120" sortable>
        <template scope="scope">
          <span v-if="scope.row.state==0" style="color: red">禁用</span>
          <span v-else style="color: #11b95c">启用</span>
        </template>
      </el-table-column>


      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="enableEdit(scope.$index, scope.row)">启用</el-button>
          <el-button size="small" @click="disableEdit(scope.$index, scope.row)">禁用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger"  :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :page-size="pageSize" :total="totals" :current-page="pageNum" style="float:right;">
      </el-pagination>
    </el-col>



    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">

      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <!--        <h3 style="text-align: center"> 是否确认修改用户的状态 </h3>-->
        <h3 style="text-align: center" v-model="txt"> {{txt}} </h3>

      </el-form>


      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">确认</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>

export default {
  data() {
    return {

      //分页查询
      departments: [],
      pageSize: 5,
      pageNum: 1,
      totals: 0,
      listLoading: false,

      //高级查询
      keyword: "",


      total: 0,
      page: 1,
      sels: [],//列表选中列

      title: "",
      txt: "",

      saveFormVisible: false,//编辑界面是否显示
      saveLoading: false,
      saveFormRules: {
        username: [
          {required: true, message: '请输入名称', trigger: 'blur'}
        ]
      },


      //编辑界面数据
      saveForm: {
        id: null,
        username: "",
        phone: '',
        state: 1,
        age: "",
        headImg: ""
      },
      managers: [],
      deptTree: []

    }
  },
  methods: {


    handleCurrentChange(val) {
      this.pageNum = val;
      this.getDepartments();
    },

    //高级查询
    keywordQuery() {
      this.pageNum = 1;
      this.getDepartments();
    },


    //获取用户列表
    getDepartments() {
      let para = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        keyword: this.keyword
      };
      this.listLoading = true;
      //NProgress.start();

      //发送请求开始
      this.$http.post("/user", para).then(res => {
        this.departments = res.data.rows;
        this.totals = res.data.totals;
        this.listLoading = false;
      }).catch(res => {
        this.$message.error("系统繁忙,请稍后再试")
      })
      //发送请求结束
    },


    enableEdit: function (index, row) {
      this.title = "启用",
          this.txt = "是否确认修改用户的状态为 启用";
      this.saveFormVisible = true;
      this.saveForm = Object.assign({}, row);
      this.saveForm.state = 1
    },

    disableEdit: function (index, row) {
      this.title = "禁用"
      this.txt = "是否确认修改用户的状态为 禁用";
      this.saveFormVisible = true;
      this.saveForm = Object.assign({}, row);
      this.saveForm.state = 0
    },

    //编辑
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认修改吗？', '提示', {}).then(() => {
            this.saveLoading = true;
            let para = Object.assign({}, this.saveForm);
            console.log(para)
            this.$http.put("/user", para).then(res => {
              //  关闭加载框
              this.saveLoading = false;
              //  关闭对话框
              this.saveFormVisible = false;
              if (res.data.success) {
                this.$message.success("成功")
                console.log(res.data)
                this.getDepartments()
              } else {
                this.$message.error("系统繁忙,请稍后再试");
              }
            }).catch(res => {
              this.$message.error("系统繁忙,请稍后再试");
            });

          });
        }
      });
    },


    selsChange: function (sels) {
      this.sels = sels;
    },

  },
  mounted() {
    this.getDepartments();
  }
}

</script>

<style scoped>

</style>